import json
import os
from typing import Dict, Any

class ConfigManager:
    """配置管理器"""
    
    def __init__(self, config_dir="config"):
        self.config_dir = config_dir
        self.config_file = os.path.join(config_dir, "simulation_config.json")
        
    def save_config(self, config: Dict[str, Any]) -> bool:
        """保存配置到文件"""
        try:
            os.makedirs(self.config_dir, exist_ok=True)
            with open(self.config_file, 'w', encoding='utf-8') as f:
                json.dump(config, f, indent=4, ensure_ascii=False)
            return True
        except Exception as e:
            print(f"保存配置失败: {e}")
            return False
    
    def load_config(self) -> Dict[str, Any]:
        """从文件加载配置"""
        try:
            if os.path.exists(self.config_file):
                with open(self.config_file, 'r', encoding='utf-8') as f:
                    return json.load(f)
            return {}
        except Exception as e:
            print(f"加载配置失败: {e}")
            return {}
    
    def get_default_config(self) -> Dict[str, Any]:
        """获取默认配置"""
        return {
            "orbit": {
                "altitude": 550,
                "inclination": 53
            },
            "communication": {
                "tx_power": 30,
                "frequency": 2.4,
                "bandwidth": 1e6
            },
            "visualization": {
                "update_interval": 100,
                "max_history_points": 100
            }
        }